import { Singleton } from '../../framework/common/Singleton';
import i18n from '../../../../i18n';
import { RemoteMgr } from './RemoteMgr';
import type { DialogData } from '../../../../shares/sharesApp';
import { RemoteType } from '../../../../shares/sharesApp';
import { EventEmitter } from '../../framework/common/EventEmitter';
import { PlayerMgr } from './PlayerMgr';
import { GameMgr } from './GameMgr';
import { GameProgress } from '../const/GameProgress';

export class InteractMgr extends Singleton<InteractMgr>() {
    /**
     * 初始化 / Initialize
     */
    public start(): void {
        this.bindEvents();
    }
    /**
     * 绑定事件 / Bind events
     */
    bindEvents() {
        RemoteMgr.instance.onServerEvent((event) => {
            if (event.args.type === RemoteType.DIALOG_END) {
                EventEmitter.instance.emit(
                    `dialogEnd_${event.entity.player.userId}`,
                    event.entity
                );
                console.log('(Server) Client dialogEnd');
            }
        });
    }
    /**
     * 对话 / Dialog
     * @param id 对话ID
     * @param entity 玩家实体
     * @param event 事件
     */
    public dialog(
        id: string,
        entity: GamePlayerEntity,
        event?: GameInteractEvent
    ): unknown {
        // const player = PlayerMgr.instance.getPlayer(entity.player.userId);
        switch (id) {
            case 'jack_o_lantern':
                this.jack_o_lantern(entity);
                break;
            case 'witch':
                this.witch(entity);
                break;
            case 'candy':
                this.candy(entity, event!);
                break;
            case 'choose_poison':
                return this.choose_poison(entity);
            default:
                throw new Error('Invalid dialog id');
        }
    }
    /**
     * 南瓜灯
     * @param entity
     */
    private jack_o_lantern(entity: GamePlayerEntity): void {
        const player = PlayerMgr.instance.getPlayer(entity.player.userId);
        RemoteMgr.instance.send<DialogData>(entity, {
            type: RemoteType.DIALOG,
            data: {
                title: i18n.t('npc.jack_o_lantern.title', { lng: player?.lng }),
                content: [
                    i18n.t('npc.jack_o_lantern.content', { lng: player?.lng }),
                ],
            },
        });
        EventEmitter.instance.once(`dialogEnd_${player?.userId}`, async () => {
            const result = await entity.player.dialog<GameSelectDialogParams>({
                type: GameDialogType.SELECT,
                title: i18n.t('npc.jack_o_lantern.title', {
                    lng: player?.lng,
                }),
                content: i18n.t('npc.jack_o_lantern.ask', {
                    lng: player?.lng,
                }),
                options: [
                    i18n.t('npc.jack_o_lantern.options.1', {
                        lng: player?.lng,
                    }),
                    i18n.t('npc.jack_o_lantern.options.2', {
                        lng: player?.lng,
                    }),
                    i18n.t('npc.jack_o_lantern.options.3', {
                        lng: player?.lng,
                    }),
                ],
            });

            switch (result?.index) {
                case 0:
                    break;
                case 1:
                    break;
                case 2:
                    break;
                default:
                    break;
            }
        });
    }
    /**
     * 女巫
     * @param entity
     */
    private witch(entity: GamePlayerEntity): void {
        const player = PlayerMgr.instance.getPlayer(entity.player.userId);
        // entity.player.dialog<GameTextDialogParams>({
        //     type: GameDialogType.TEXT,
        //     title: i18n.t('npc.witch.title'),
        //     content: i18n.t('npc.witch.content'),
        // });
        RemoteMgr.instance.send<DialogData>(entity, {
            type: RemoteType.DIALOG,
            data: {
                title: i18n.t('npc.witch.title', { lng: player?.lng }),
                content: [i18n.t('npc.witch.content', { lng: player?.lng })],
            },
        });
        EventEmitter.instance.once(`dialogEnd_${player?.userId}`, async () => {
            const result = await entity.player.dialog<GameSelectDialogParams>({
                type: GameDialogType.SELECT,
                title: i18n.t('npc.witch.title', { lng: player?.lng }),
                content: i18n.t('npc.witch.ask', { lng: player?.lng }),
                options: [
                    i18n.t('npc.witch.options.1', { lng: player?.lng }),
                    i18n.t('npc.witch.options.2', { lng: player?.lng }),
                ],
            });
            if (result) {
                switch (result.index) {
                    case 0:
                        RemoteMgr.instance.send<DialogData>(entity, {
                            type: RemoteType.DIALOG,
                            data: {
                                title: i18n.t('npc.witch.title', {
                                    lng: player?.lng,
                                }),
                                content: [
                                    i18n.t('npc.witch.option_1.content.1', {
                                        lng: player?.lng,
                                    }),
                                    i18n.t('npc.witch.option_1.content.2', {
                                        lng: player?.lng,
                                    }),
                                    i18n.t('npc.witch.option_1.content.3', {
                                        lng: player?.lng,
                                    }),
                                    i18n.t('npc.witch.option_1.content.4', {
                                        lng: player?.lng,
                                    }),
                                    i18n.t('npc.witch.option_1.content.5', {
                                        lng: player?.lng,
                                    }),
                                    i18n.t('npc.witch.option_1.content.6', {
                                        lng: player?.lng,
                                    }),
                                ],
                            },
                        });
                        break;
                    case 1:
                        RemoteMgr.instance.send<DialogData>(entity, {
                            type: RemoteType.DIALOG,
                            data: {
                                title: i18n.t('npc.witch.option_2.title', {
                                    lng: player?.lng,
                                }),
                                content: [
                                    i18n.t('npc.witch.option_2.content.1', {
                                        lng: player?.lng,
                                    }),
                                    i18n.t('npc.witch.option_2.content.2', {
                                        lng: player?.lng,
                                    }),
                                ],
                            },
                        });
                }
            }
        });
    }

    /**
     * 糖果
     * @param entity
     * @param event
     */
    private async candy(
        entity: GamePlayerEntity,
        event: GameInteractEvent
    ): Promise<void> {
        const player = PlayerMgr.instance.getPlayer(entity.player.userId);
        // 如果在选择毒药阶段
        if (GameMgr.instance.progress === GameProgress.ChoosePoison) {
            // 如果可以作为毒药
            if (GameMgr.instance.canChooseCandyAsPoison(entity.player.userId)) {
                const result =
                    await entity.player.dialog<GameSelectDialogParams>({
                        type: GameDialogType.SELECT,
                        title: i18n.t('npc.candy.title', { lng: player?.lng }),
                        content: i18n.t('npc.candy.poison', {
                            lng: player?.lng,
                        }),
                        options: [
                            i18n.t('base.ok', { lng: player?.lng }),
                            i18n.t('base.cancel', { lng: player?.lng }),
                        ],
                    });

                if (result?.index === 0) {
                    GameMgr.instance.candyAsPoison(
                        event.targetEntity,
                        entity.player.userId
                    );
                }
            }
            // 如果在选择糖果阶段
        } else if (GameMgr.instance.progress === GameProgress.ChooseCandy) {
            // 如果是当前玩家
            if (GameMgr.instance.chooseCandyPlayer === entity.player.userId) {
                const result =
                    await entity.player.dialog<GameSelectDialogParams>({
                        type: GameDialogType.SELECT,
                        title: i18n.t('npc.candy.title', { lng: player?.lng }),
                        content: i18n.t('npc.candy.candy', {
                            lng: player?.lng,
                        }),
                        options: [
                            i18n.t('base.ok', { lng: player?.lng }),
                            i18n.t('base.cancel', { lng: player?.lng }),
                        ],
                    });
                if (result?.index === 0) {
                    EventEmitter.instance.emit('candyEat', event.targetEntity);
                }
            }
        }
    }

    /**
     * 选择毒药
     * @param entity
     */
    private async choose_poison(entity: GamePlayerEntity): Promise<boolean> {
        const player = PlayerMgr.instance.getPlayer(entity.player.userId);
        const result = await entity.player.dialog<GameSelectDialogParams>({
            type: GameDialogType.SELECT,
            content: i18n.t('game.choose_poison', { lng: player?.lng }),
            options: [
                i18n.t('base.yes', { lng: player?.lng }),
                i18n.t('base.no', { lng: player?.lng }),
            ],
        });

        return result?.index === 0;
    }
}
